<template>
  <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input
        v-model="form.name"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input
        v-model="form.code"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
        <el-option
          v-for="item in list"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input
        v-model="form.introduce"
        style="width: 80%"
        placeholder="1-300个字符"
        type="textarea"
        :rows="3"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="$emit('close')">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import {
  addDepartments,
  getDepartDetail,
  updateDepartments
} from '@/api/department'
export default {
  props: {
    pid: {
      required: true,
      type: String
    },
    isEdit: {
      required: true,
      type: Boolean
    },
    originList: {
      required: true,
      type: Array
    }
  },
  // watch: {
  //   'id': function(newVal, oldVal) {
  //     console.log('id变了！ 新值：', newVal, '旧值', oldVal)
  //     // 重发请求获取详情
  //     this.loadEmployeeSimple()
  //   }
  // },
  data() {
    const validCode = (rule, value, callback) => {
      console.log('qwe', value)
      // if (this.originList.find(item=>item.code ===value)) {
      let existCodeList = this.originList.map(item => item.code) // 只有code的数组
      if (this.isEdit) {
        const curDep = this.originList.find(item => item.id === this.pid)
        const curCode = curDep.code
        existCodeList = existCodeList.filter(item => item !== curCode)
      }
      if (existCodeList.includes(value)) {
        callback(new Error(value + '编码集不能重复'))
      } else {
        callback()
      }
    }
    const validName = (rules, value, callback) => {
      // 筛选出子部门的name
      let nameList = this.originList
        .filter((item) => item.pid === this.pid)
        .map((item) => item.name)
      // 如果是编辑状态，不能取和同级部门一样的名字
      if (this.isEdit) {
        // 获取到pid，pid相同的就是同级部门
        const pid = this.originList.find((item) => item.id === this.pid).pid
        // 筛选出同级部门的name
        nameList = this.originList
          .filter((item) => item.pid === pid && item.id !== this.pid)
          .map((item) => item.name)
      }
      if (nameList.includes(value)) {
        callback(new Error('部门名称已存在'))
      } else {
        callback
      }
    }

    return {
      list: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadEmployeeSimple()

    this.loadDetail()
  },
  methods: {

    resetForm() {
      this.$refs.deptForm.resetFields()
    },
    async loadDetail() {
      if (this.isEdit) {
        const res = await getDepartDetail(this.pid)
        console.log('获取', res)
        this.form = res.data
      }
    },
    doAdd() {
      const data = { ...this.form, pid: this.pid }
      addDepartments(data)
      this.$emit('success')
    },
    async doEdit() {
      await updateDepartments(this.form)
      this.$emit('success')
    },
    hSubmit() {
      this.$refs.deptForm.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    async loadEmployeeSimple() {
      const res = await getEmployeeSimple()
      console.log(res)
      this.list = res.data
    }
  }
}
</script>

<style>
</style>
